/**
 * optionsconsoledialog.cpp is part of %PROJECTNAME%.
 *
 * %PROJECTNAME% is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * %PROJECTNAME% is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with %PROJECTNAME%.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "menuconsoledlg.h"
#include "askforconsoledlg.h"
#include <iostream>
#include <sstream>
#include <boost/foreach.hpp>

///////////////////////////////////////////////////////////////
MenuConsoleDlg::MenuConsoleDlg( const tOptions & a_options ) :
    Printable(),
    m_options( a_options )
{
}

///////////////////////////////////////////////////////////////
MenuConsoleDlg::~MenuConsoleDlg()
{
    m_options.clear();
}

///////////////////////////////////////////////////////////////
std::string MenuConsoleDlg::Print() const
{
    std::ostringstream strStream;

    tOption optionIdx = 0;
    BOOST_FOREACH( const std::string & option, m_options )
    {
        strStream << optionIdx << " - " << option << std::endl;
        ++optionIdx;
    }

    return strStream.str();
}

///////////////////////////////////////////////////////////////
tOption MenuConsoleDlg::GetOption()
{
    tOption option = -1;

    if( !m_options.empty() )
    {
        std::cout << Print();

        tOption nOptions = m_options.size();
        do
        {
            AskForConsoleDlg< tOption > dlg( "Select an option: " );
            option = dlg.AskFor();
        }
        while( option < 0 || option >= nOptions );
    }

    return option;
}
